package cn.sfrog.common.utils.file;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * 文件类型工具类
 */
public class FileTypeUtils {

    public static final int NUMBER_ONE = 1;
    public static final int NUMBER_TWO = 2;
    public static final int NUMBER_THREE = 3;
    public static final int NUMBER_FOUR = 4;
    public static final int NUMBER_FIVE = 5;
    public static final int NUMBER_SIX = 6;
    public static final int NUMBER_SEVEN = 7;
    public static final int NUMBER_EIGHT = 8;
    public static final int NUMBER_NINE = 9;
    public static final int NUMBER_FIFTY_FIVE = 55;
    public static final int NUMBER_FIFTY_SIX = 56;
    public static final int NUMBER_FIFTY_SEVEN = 57;
    public static final int NUMBER_SIXTY_SIX = 66;
    public static final int NUMBER_SEVENTY = 70;
    public static final int NUMBER_SEVENTY_ONE = 71;
    public static final int NUMBER_SEVENTY_THREE = 73;
    public static final int NUMBER_SEVENTY_FOUR = 74;
    public static final int NUMBER_SEVENTY_SEVEN = 77;
    public static final int NUMBER_SEVENTY_EIGHT = 78;
    public static final int NUMBER_EIGHTY = 80;
    public static final int NUMBER_NINETY_SEVEN = 97;

    /**
     * 获取文件类型
     * <p>
     * 例如: app.txt, 返回: txt
     *
     * @param file 文件名
     * @return 后缀（不含".")
     */
    public static String getFileType(File file) {
        if (null == file) {
            return StringUtils.EMPTY;
        }
        return getFileType(file.getName());
    }

    /**
     * 获取文件类型
     * <p>
     * 例如: app.txt, 返回: txt
     *
     * @param fileName 文件名
     * @return 后缀（不含".")
     */
    public static String getFileType(String fileName) {
        int separatorIndex = fileName.lastIndexOf(".");
        if (separatorIndex < 0) {
            return "";
        }
        return fileName.substring(separatorIndex + 1).toLowerCase();
    }

    /**
     * 获取文件类型
     *
     * @param photoByte 文件字节码
     * @return 后缀（不含".")
     */
    public static String getFileExtendName(byte[] photoByte) {
        String strFileExtendName = "JPG";
        if ((photoByte[0] == NUMBER_SEVENTY_ONE) && (photoByte[1] == NUMBER_SEVENTY_THREE) && (photoByte[NUMBER_TWO] == NUMBER_SEVENTY) && (photoByte[NUMBER_THREE] == NUMBER_FIFTY_SIX)
                && ((photoByte[NUMBER_FOUR] == NUMBER_FIFTY_FIVE) || (photoByte[4] == NUMBER_FIFTY_SEVEN)) && (photoByte[NUMBER_FIVE] == NUMBER_NINETY_SEVEN)) {
            strFileExtendName = "GIF";
        } else if ((photoByte[NUMBER_SIX] == NUMBER_SEVENTY_FOUR) && (photoByte[NUMBER_SEVEN] == NUMBER_SEVENTY) && (photoByte[NUMBER_EIGHT] == NUMBER_SEVENTY_THREE) && (photoByte[NUMBER_NINE] == NUMBER_SEVENTY)) {
            strFileExtendName = "JPG";
        } else if ((photoByte[0] == NUMBER_SIXTY_SIX) && (photoByte[1] == NUMBER_SEVENTY_SEVEN)) {
            strFileExtendName = "BMP";
        } else if ((photoByte[1] == NUMBER_EIGHTY) && (photoByte[NUMBER_TWO] == NUMBER_SEVENTY_EIGHT) && (photoByte[NUMBER_THREE] == NUMBER_SEVENTY_ONE)) {
            strFileExtendName = "PNG";
        }
        return strFileExtendName;
    }
}